package com.edroid.apkhooker.db;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.Context;

/**
 * Assets工具
 * 
 * @author zengming 2014/12/16
 *
 */
public class AssetsReleaseTool {
	/**
	 * 释放一个Assets目录下的文件到files目录下
	 * 
	 * @param context
	 * @param filename
	 * @return files目录下此文件的完整路径
	 * @throws IOException
	 */
	public static String releaseFile(Context context, String filename)
			throws IOException {
		File outFile = context.getFileStreamPath(filename);
		if (!outFile.exists()) {
			InputStream is = context.getAssets().open(filename);
			OutputStream out = context.openFileOutput(filename,
					Context.MODE_PRIVATE);
			byte[] buf = new byte[1024 * 4];
			int len = 0;
			while ((len = is.read(buf)) != -1) {
				out.write(buf, 0, len);
			}
			is.close();
			out.close();
		}
		return outFile.getAbsolutePath();
	}
}
